/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.saar.penguin.give2.mapviewer;

import give.formula.Atom;
import give.world.World;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author koller
 */
public class MapViewerTooltipOverlay implements MouseOverObjectListener, MapAnnotationPlugin {
    private int x, y;
    private List<String> strings;
    private boolean display;
    private World world;
    private JMapViewer viewer;

    public MapViewerTooltipOverlay(World world, JMapViewer viewer) {
//        setOpaque(false);
        this.world = world;
        this.viewer = viewer;
        display = false;
        strings = new ArrayList<String>();
    }

    public void mouseEnteredObject(int x, int y, Collection<String> objects) {
        if (objects.isEmpty()) {
            display = false;
            viewer.repaint();
        } else {
            this.x = x;
            this.y = y;
            makeTooltipStrings(objects);
            display = true;

            viewer.repaint();
        }
    }

    private void makeTooltipStrings(Collection<String> objects) {
        List<String> controls = new ArrayList<String>();

        strings.clear();

        for (String o : objects) {
            StringBuffer buf = new StringBuffer(" " + o + ": " + world.getType(o));

            String state = world.getState(o);
            if( state != null ) {
                buf.append(", state=" + state);
            }

            controls.clear();
            for( Atom a : world.getTrueAtoms() ) {
                if( a.getPredicate().equals("controls") && a.getArgumentAsString(0).equals(o) ) {
                    controls.add(a.getArgumentAsString(1));
                }
            }
            if( ! controls.isEmpty() ) {
                buf.append(", controls=" + controls);
            }

            buf.append(" ");
            strings.add(buf.toString());
        }
    }

//    @Override
    private void paintComponent(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;

        if (display) {
            FontMetrics fm = graphics.getFontMetrics();
            Rectangle2D stringRect = fm.getStringBounds(strings.get(0), g);

            // determine maximum width of strings
            double width = stringRect.getWidth();
            for( String s : strings ) {
                width = Math.max(width, fm.getStringBounds(s, g).getWidth() );
            }

            // adjust starting x position to the left to make sure that strings fit in window
            double startx = stringRect.getX() + x;
            if( startx + width > viewer.getWidth() ) {
                startx = viewer.getWidth() - width;
            }


            Rectangle2D rect = new Rectangle2D.Double(startx, stringRect.getY() + y, width, strings.size() * stringRect.getHeight());

            graphics.setColor(Color.white);
            graphics.fill(rect);

            graphics.setColor(Color.black);
            graphics.draw(rect);

            int yoff = 0;
            for (String s : strings) {
                graphics.drawString(s, (int) startx, y + yoff);
                yoff += stringRect.getHeight();
            }

        }
    }

    public void paint(Graphics2D graphics, JMapViewer mapViewer) {
        paintComponent(graphics);
    }
}
